草庐IT

struct - 戈朗 : type conversion between slices of structs

全部标签

json - 在 Go 中使用 struct 获取 json 数据

我在Go中遇到了一个相当简单的问题,因为我对它完全陌生。我想从RESTapi获取和打印数据。我写的代码:packagemainimport(_"bytes""encoding/json""fmt""io/ioutil""net/http")typeHeadersstruct{HeadersHttpHeaders`json:"headers"`}typeHttpHeadersstruct{AcceptstringAccept_Encodingstring`json:"Accept-Encoding"`Accept_Languagestring`json:"Accept-Language"

go - 如何调用 golang struct 字段的函数?

packagemainimport"fmt"import"reflect"typeTstruct{}func(t*T)Foo(){fmt.Println("foo")}typeAstruct{TsT}funcmain(){vartTvaraA=A{Ts:t}val:=reflect.ValueOf(&a).Elem()fori:=0;i$gorunreflect_call_1.go*main.Tptrpanic:reflect:callofreflect.Value.CallonzeroValuegoroutine1[running]:reflect.flag.mustBe(0x0,0

go - 验证在 struct golang 中设置了字段

我有以下结构:typeFoostruct{Bar*FooBarBaz*FooBaz}typeFooBarstruct{Namestring}typeFooBazstruct{Namestring}如何访问结构中的Baz和Bar而不会在未设置时获取nil指针引用?我想要如下所示的内容,但我不断收到nil指针取消引用错误。ifFoo.Bar==nil{throwerror}我正在为此苦苦挣扎! 最佳答案 您应该能够与nil进行比较,这是一个有效的示例:check:=func(fFoo){iff.Bar==nil{panic("oops!

pointers - []struct{} 和 []*struct{} 有什么区别?

下面有什么区别?typeDemostruct{sstring}funcgetDemo1()([]*Demo)//1funcgetDemo2()([]Demo)//2getDemo1和getDemo2在内存上有区别吗? 最佳答案 我要回答这个问题,尽管我的判断更好,只是将OP发送到导览和文档/规范。主要是因为:IsthereanymemorydifferencebetweengetDemo1andgetDemo2?这个具体问题的答案取决于您如何使用slice。Go是按值传递,因此传递结构值会复制它们。例如,请考虑以下示例。https:

go - 给定另一个动态修改 Golang struct{} 值的属性

我在路由器处理程序中有这段代码decoder:=json.NewDecoder(r.Body)vartperson.Modelerr:=decoder.Decode(&t).//tisastructvalueitem,ok:=v.People[params["id"]].//okisastructvalueift.Handle!=""{item.Handle=t.Handle}ift.Work!=""{item.Work=t.Work}ift.Image!=""{item.Image=t.Image}ift.Firstname!=""{item.Firstname=t.Firstnam

xml - 如何更改 struct XML 标签?

我从亚马逊检索到2段非常相似的XML。A1F83G8C2ARO7P0195019199New2018-11-07T02:05:14.342Z4514130fef8c86d-c563-4373-81c9-78dcf691283c我目前使用自定义类型将其解码并自定义解码为如下所示的结构:typeLowestPricedPricedOffersstruct{ErrorAmazonError`xml:"Error"`Allstruct{/*TheonlywayIfoundtoretrieve'status'fromtheGetLowestPricedOffersForASINResultele

go - 我想知道如何将 set struct 实现为映射值

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭4年前。Improvethisquestion我想在golang上使用set作为映射值。所以我这样编码:import("fmt""reflect")typeTestSetstruct{Items[]Test}func(ts*TestSet)Add(t*Test){ok:=truefor_,item:=rangets.Items{ifitem.Equal(t){ok=falsebreak}}ifok{ts.Items=append(ts.Items,*

struct - 将结构分配给另一个结构

我有一个RegistrationRequest结构:typeRegistrationRequeststruct{Email*stringEmail2*stringUsername*stringPassword*stringNamestring}其中Email2是再次输入的电子邮件值,以验证用户输入的内容是否正确。我还有一个用户结构:typeUserstruct{Email*stringUsername*stringPassword*stringNamestring}当然,除了注册之外,不需要存储Email2。所以我有两个变量:req和u-每个结构一个。是否可以将req结构分配给u结构,

Go Error : panic: runtime error: invalid memory address or nil pointer dereference. Changing map inside a struct which is present in 另一个结构,

这个问题在这里已经有了答案:map[string]*type"invalidmemoryaddressornilpointerdereference"(1个回答)关闭3个月前。我必须结构让我们说struct1和struct2,struct2包含一个带有struct1的映射,struct1也包含一个映射,我想更改struct1中存在的映射。这是抛出一个运行时错误:panic:运行时错误:无效内存地址或零指针解引用typeFailureDatastruct{failuresInCommitsmap[string][]string}typeDetectionResultsstruct{Fai

戈朗 : Parse bit values from a byte

我需要解析一个由两个字节组成的网络数据包:第一个由8位组成,根据它们的顺序设置某些标志(例如),第二个是uint8(很简单)1-在线0-不活跃1-漂亮1-很帅0-秃头0-聋人0-静音0-盲我如何从字节原语中解析它? 最佳答案 一些用于处理二进制文件的有用的Go标准库包:encoding/binarymath/bits要从字节中提取单个位,您应该使用按位运算符-|、&和>>>。Forexample:packagemainimport("fmt")funcmain(){v:=byte(0xB2)if(v>>4)&1==1{fmt.Pri